package com.findviewbydream.evil.eda.View;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.Switch;

import com.findviewbydream.evil.eda.Utils.Utils;

/********************************************
 * 工程名：Eda
 * 包名：com.findviewbydream.evil.eda.View
 * 时间：16-7-22 上午11:41
 * 作者：白雨濃 evil
 * 邮箱：evilbai@foxmail.com
 * 内容：垂直滚动
 ********************************************/
public class LScrollView extends ScrollView
{
    private LinearLayout layout;

    private ViewGroup up;
    private ViewGroup down;

    private boolean first = true;
    private int height;

    public LScrollView(Context context)
    {
        super(context);
        height = getSreenHeight(context);
    }

    public LScrollView(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        height = getSreenHeight(context);
    }

    public LScrollView(Context context, AttributeSet attrs, int defStyleAttr)
    {
        super(context, attrs, defStyleAttr);
        height = getSreenHeight(context);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public LScrollView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)
    {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    /**
     * 获取屏幕高度
     */
    private int getSreenHeight(Context context)
    {
        int height;
        WindowManager windowManager = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics metrics = new DisplayMetrics();
        windowManager.getDefaultDisplay().getMetrics(metrics);
        height = metrics.heightPixels;
        return height;
    }

    /**
     * 决定内部View的宽高
     *
     * @param widthMeasureSpec
     * @param heightMeasureSpec
     */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
    {
        if (first)
        {
            layout = (LinearLayout) getChildAt(0);

            up = (ViewGroup) layout.getChildAt(0);
            down = (ViewGroup) layout.getChildAt(1);

            up.getLayoutParams().height = height;
            down.getLayoutParams().height = height;

            first = false;
        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    /**
     * 决定子View的位置
     *
     * @param changed
     * @param l
     * @param t
     * @param r
     * @param b
     */
    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b)
    {
        super.onLayout(changed, l, t, r, b);
    }

    /**
     * 滚动事件
     *
     * @param ev
     * @return
     */
    @Override
    public boolean onTouchEvent(MotionEvent ev)
    {
        switch (ev.getAction())
        {
            case MotionEvent.ACTION_UP:
                int scroolY = getScrollY();
                if (scroolY > height / 5)
                {
                    this.smoothScrollTo(0, height*2);
                }
                else
                {
                    this.smoothScrollTo(0, 0);
                }
                return true;
        }
        return super.onTouchEvent(ev);
    }
}
